import { Inject, Get, Query } from '@midwayjs/core';
import {
  CoolController,
  BaseController,
  CoolTag,
  TagTypes,
} from '@cool-midway/core';
import { AutomationTaskEntity } from '../../entity/task';
import { AutomationTaskService } from '../../service/app/task';

/**
 * App端-自动化任务
 */
@CoolController({
  api: ['list', 'info'],
  entity: AutomationTaskEntity,
  service: AutomationTaskService,
})
export class AppAutomationTaskController extends BaseController {
  @Inject()
  automationTaskService: AutomationTaskService;

  /**
   * 根据流水线ID获取任务列表
   */
  @Get('/byPipeline', { summary: '根据流水线ID获取任务列表' })
  async getTasksByPipeline(@Query('pipelineId') pipelineId: number) {
    const tasks = await this.automationTaskService.getTasksByPipeline(
      pipelineId
    );
    return this.ok(tasks);
  }

  /**
   * 根据设备ID获取任务列表
   */
  @Get('/byDevice', { summary: '根据设备ID获取任务列表' })
  async getTasksByDevice(@Query('deviceId') deviceId: string) {
    const tasks = await this.automationTaskService.getTasksByDevice(deviceId);
    return this.ok(tasks);
  }

  /**
   * 获取操作类型选项
   */
  @Get('/actionTypes', { summary: '获取操作类型选项' })
  async getActionTypes() {
    return this.ok([
      { label: '开', value: 0 },
      { label: '关', value: 1 },
      { label: '完全同步', value: 2 },
    ]);
  }

  /**
   * 获取任务统计
   */
  @Get('/statistics', { summary: '获取任务统计' })
  async getStatistics() {
    const statistics = await this.automationTaskService.getTaskStatistics();
    return this.ok(statistics);
  }
}
